package com.ihrm.company.controller;

import com.ihrm.common.entity.Result;
import com.ihrm.company.service.CompanyService;
import com.ihrm.domain.company.Company;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    @PostMapping("save")
    public Result<Void> saveCompany(@RequestBody Company company){
        return this.companyService.addCompany(company);
    }

    @PutMapping("update/{id}")
    public Result<Void> updateCompany(@PathVariable("id") String id, @RequestBody Company company){
        return this.companyService.updateCompany(id, company);
    }

    @DeleteMapping("delete/{id}")
    public Result<Void> deleteCompany(@PathVariable("id") String id){
        return this.companyService.deleteById(id);
    }

    @GetMapping("find/{id}")
    public Result<Company> findCompanyById(@PathVariable("id") String id){
        return this.companyService.findById(id);
    }

    @GetMapping("findAll")
    public Result<Page<Company>> findAll(@RequestParam(value = "page", required = true, defaultValue = "1") int page,
                                         @RequestParam(value = "rows", required = true, defaultValue = "10") int rows,
                                         @RequestParam(value = "key", required = false) String key,
                                         @RequestParam(value = "field", required = false) String field,
                                         @RequestParam(value = "sort", required = false, defaultValue = "true") Boolean sort){
        return this.companyService.findAll(page - 1, rows, key, field, sort);
    }
}
